var ns;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        ProjectName:'',
        projectList: [], 
        form: {
            startMonth: '', 
            activeDateDesc: '月份',
            ProjectCompanyType: 1,
            ProjectID:0,
            ProjectIDs: [],
        },
        list: [],
        IsAnalysis:false,
        minDate: new Date(),
        maxDate: new Date(),
        currentDate:0,
    },
    methods: {
        formatter(type, val) {
            if (type === 'year') {
                return `${val}年`;
            }
            if (type === 'month') {
                return `${val}月`;
            }
            return val;
        },
        onConfirm(day) {
            var that = this;
            var nowdate=moment(day).format('YYYY-MM');
            that.form.startMonth = nowdate;
            that.doSearch();
        },
        doSelectProject: function (ID,Name) {
            var that = this;
            that.ProjectName=Name;
            that.form.ProjectID=ID;
            that.form.ProjectIDs=[];
            that.form.ProjectIDs.push(ID);
            that.showsearchparam = false;
            that.doSearch();
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
               
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            if (type == 2) { //按月 用的van-popup 自带了遮罩
                that.showsearch=false;
            }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.get_data();
        },
         //筛选数据
         getSearchParams: function () {
            var that = this;
            var options = {};
            options.action = "gettaskanalysisparams";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.projectList = data.projectList;
                    if(that.projectList!=undefined&&that.projectList.length>0&&that.form.ProjectID<=0){
                        that.doSelectProject(that.projectList[0].ID,that.projectList[0].Name); 
                    }
                    else{
                        that.get_data();
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        get_data: function() {
            var that = this;
            var options = {};
            options.action = "getmonthcleaning";
            options.Month =that.form.startMonth+'-01';
            options.ProjectID = JSON.stringify(that.form.ProjectIDs);
            options.IsAnalysis=that.IsAnalysis;
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.list = data.Detailed;     
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        open_list: function(item) {
            var that=this;
            var day=that.form.startMonth+'-'+item.Date;
            if(item.Date<10){
                day=that.form.startMonth+'-0'+item.Date;
            }
            var url = 'bjlh_tasklist_record_frm';
            ns.openWin(url, "临时工作记录列表", {
                TaskType:3,
                Day:day,
                IsAnalysis:that.IsAnalysis,
                ProjectID:that.form.ProjectID,
            });
        },
        onClickOpen:function(item){
            var that = this;
            item.IsOpen=!item.IsOpen;
        },
    },
    
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.getSearchParams();
    app.form.ProjectID=api.pageParam.ProjectID || 0;
    app.ProjectName=api.pageParam.ProjectName || '';
    app.IsAnalysis=api.pageParam.IsAnalysis || false;//是否是报表跳转的
    app.form.startMonth=api.pageParam.startMonth || '';  
    if( app.form.ProjectID){
        app.form.ProjectIDs=[];
        app.form.ProjectIDs.push(app.form.ProjectID);
    }
   
    app.minDate=new Date(moment(new Date()).add(-20, 'y'));
    app.maxDate=new Date(moment(new Date()).add(50, 'y'));
    app.currentDate=new Date();
    var nowdate=moment(app.currentDate).format('YYYY-MM');
    if(app.form.startMonth){
        app.currentDate=new Date(app.form.startMonth);
    }
    if(!app.form.startMonth){
        app.form.startMonth=nowdate;
    }
  
    // app.get_data();
};

